<template>
	<view class="content">
		<!-- <view class="btn1" @click="goIndex" v-if="addressType"> 完成地址选择 </view> -->
		<radio-group name="radio">
			<view class="box" v-for="(item, index) in AddressList" :key="index">
				<view class="box_left">
					<label><radio value="radio" @click="selectRadio(item.id)" /></label>
				</view>
				<view class="box_center" @click="alter(item.id)">
					<text>{{ item.consignee }}</text>
					<text style="margin-left: 20upx;">{{ item.phone }}</text>
					<text v-if="item.default === '1'" style="color: #309DEC;margin-left: 30upx; border: 1upx #9bdae2 solid; padding:3upx 20upx; border-radius: 10upx;">默认</text>
					<br />
					<text style="color: #7D7D7D;">{{ item.province }}{{ item.city }}{{ item.district }} {{ item.details }}</text>
				</view>
				<view class="box_right"></view>
			</view>
		</radio-group>
		<button class="btn" @click="goEditAdress">新增地址</button>
		<uni-load-more :loadingType="loadingType">下拉显示更多</uni-load-more>
	</view>
</template>

<script>
import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
import eonfox from '@/components/eonfox/eonfox.js';
import fns from '@/components/eonfox/fns.js';
var ef = new eonfox();
export default {
	data() {
		return {
			AddressList: [],
			loadingType: 0,
			page: 10,
			addressType: '',
			addressID: ''
		};
	},
	components: {
		uniLoadMore
	},
	onReachBottom() {
		if (this.loadingType !== 0) {
			return;
		}
		this.loadingType = 1;
		var _this = this;
		var n;
		if (this.loadingType) {
			_this.page = _this.page + 10;
		}
		ef.submit({
			request: {
				s: ['USERADDRESSSELFLIST', [{ size: _this.page }]]
			},
			callback: function(data) {
				const dataList = fns.checkError(data, ['s'], function(errno, error) {
					uni.showToast({
						title: error,
						icon: 'none'
					});
				});
				console.log('more', dataList);

				if (dataList.s) {
					_this.AddressList = dataList.s;
					if (dataList.s.length < _this.page) {
						_this.loadingType = 2;
					} else {
						_this.loadingType = 0;
					}
				} else {
					_this.loadingType = 2;
				}
			},
			error(err) {
				fns.err('err', err, 1);
			}
		});
	},
	onLoad(e) {
		var that = this;
		console.log('地址页面接收参数',e);
		if(e.type){
			that.addressType = e.type;
		}
		
	},
	onShow() {
		var that = this;
		ef.submit({
			request: {
				addressList: ['USERADDRESSSELFLIST']
			},
			callback: function(data) {
				var dataList = fns.checkError(data, ['addressList'], function(errno, error) {
					uni.showToast({
						title: error,
						icon: 'none'
					});
				});
				console.log(dataList);
				if (dataList.addressList) {
					that.AddressList = dataList.addressList;
				}
			},
			error: function(err) {
				uni.showToast({
					title: '出错啦',
					icon: 'none'
				});
			}
		});
	},
	methods: {
		goEditAdress() {
			uni.navigateTo({
				url: '../editAddress/editAddress'
			});
		},
		alter(addressId) {
			console.log(addressId);
			uni.navigateTo({
				url: '../editAddress/editAddress?addressId=' + addressId
			});
		},
		selectRadio(addressId) {
			console.log(addressId);
			this.addressID = addressId;
			this.goIndex();
		},
		goIndex() {
			var that = this;
			console.log('addressType',that.addressType)
			if(that.addressType==99&&that.addressID){
				uni.setStorage({
					key:'nearByBusinessAddressID',
					data:that.addressID,
					success() {
						uni.switchTab({
						   url:'../../pages/nearbyBusiness/nearbyBusiness'
						})
					}
				})
			}
			else if(that.addressType==1 && that.addressID) {
				uni.redirectTo({
				   url:'../submitOrder/submitOrder?addressID='+that.addressID
				})
			} 
			else if(!that.addressType){
				
			}
			else {
				uni.showToast({
					title: '请选择地址',
					icon: 'none'
				});
			}
		}
	}
};
</script>

<style>
page {
	background-color: #fafafa;
}
.content {
	width: 100%;
	font-size: 28upx;
}
.box {
	display: flex;
	width: 96%;
	height: auto;
	margin-left: 2%;
	border-bottom: 1upx #e1e1e1 solid;
	padding-top: 20upx;
	padding-bottom: 20upx;
	line-height: 1.5em;
}
.box_left {
	width: 80upx;
	height: 100upx;
}
.box_center {
	width: 550upx;
}
.box_right {
	width: 80upx;
	height: 100upx;
}
.btn {
	width: 690upx;
	height: 80upx;
	line-height: 80upx;
	background-color: #e60b30;
	background-color: rgba( 242 155 135);
	color: #ffffff;
	font-size: 24upx;
	margin-top: 20upx;
}
.btn1 {
	width: 180upx;
	height: 60upx;
	line-height: 60upx;
	background-color: #e60b30;
	color: #ffffff;
	font-size: 24upx;
	margin-top: 30upx;
	margin-bottom: 30upx;
	border-radius: 15upx;
	text-align: center;
	float: right;
	margin-right: 20upx;
}
.box_right image {
	width: 35upx;
	height: 35upx;
	margin-top: 40upx;
	margin-left: 20upx;
}
</style>
